<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<!-- <div class="module-filter">
					<label style="margin-right: 10px">{{$t('user.language')}}</label>
					<el-select v-model="params.language" :placeholder="$t('table_data.selectText')" clearable>
						<el-option
							v-for="item in langList"
							:key="item.value"
							:label="item.name"
							:value="item.value"
							>
						</el-option>
					</el-select>
				</div> -->
				<div class="module-filter">
					<label style="margin-right: 10px">{{$t('movie_data.startDate')}}</label>
					<el-date-picker v-model="params.startDate" type="date" :editable="false" value-format="YYYY-MM-DD HH:mm:ss" :placeholder="$t('table_data.startTime')" :disabled-date="beforeOptions"></el-date-picker>
				</div>
				<div class="module-filter">
					<label style="margin-right: 10px">{{$t('movie_data.endDate')}}</label>
					<el-date-picker v-model="params.endDate" type="date" :editable="false" value-format="YYYY-MM-DD HH:mm:ss" :placeholder="$t('table_data.endTime')" :disabled-date="backOptions"></el-date-picker>
				</div>
				<div class="module-filter">
					<label style="margin-right: 10px">{{$t('movie_data.movieStatusLabel')}}</label>
					<el-select v-model="params.status" :placeholder="$t('table_data.selectText')" clearable>
						<el-option
							v-for="item in statusList"
							:key="item.value"
							:label="item.name"
							:value="item.value"
							>
						</el-option>
					</el-select>
				</div>
				<div class="module-filter">
					<el-button type="primary" icon="el-icon-search" @click="upsearch">{{ $t('table_data.searchText') }}</el-button>
					<el-button @click="reset">{{ $t('table_data.resetText') }}</el-button>
				</div>
			</div>
			<!-- <div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="searchKey" placeholder="关键词" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div> -->
		</el-header>		
		<el-main class="nopadding">
			<div class="top-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add">{{ $t('table_data.addText') }}</el-button>
				<!-- <el-button type="danger" icon="el-icon-minus" :disabled="!selection.length" @click="table_del">{{ $t('table_data.del') }}</el-button> -->
			</div>
			<scTable ref="table" :apiObj="apiObj" row-key="id" stripe border :params="params" :hideSetting="true" @sortChange="sortChange" @selection-change="selectionChange">
				<!-- <el-table-column type="selection" width="50"></el-table-column> -->
				<!-- <el-table-column :label="$t('movie_data.movieId')" prop="movieId" width="auto"></el-table-column> -->
				<el-table-column :label="$t('movie_data.fullMovieTitle')" prop="title" width="auto"></el-table-column>
				<el-table-column :label="$t('movie_data.officialSiteName')" prop="officialSiteName" width="auto"></el-table-column>
				<el-table-column :label="$t('movie_data.officialWebsite')" prop="officialSite" width="auto"></el-table-column>
				<el-table-column :label="$t('movie_data.movieStatusLabel')" prop="status" width="auto">
					<template #default="scope">
						<el-tag v-if="scope.row.status==0" type="info">{{$t('movie_data.movieStatus.status1')}}</el-tag>
						<el-tag v-if="scope.row.status==1" type="success">{{$t('movie_data.movieStatus.status2')}}</el-tag>
						<el-tag v-if="scope.row.status==2" type="warning">{{$t('movie_data.movieStatus.status3')}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column :label="$t('table_data.createAt')" prop="createAt" width="auto" sortable></el-table-column>
				<el-table-column :label="$t('movie_data.startDate')" prop="startDate" width="auto"></el-table-column>
				<el-table-column :label="$t('movie_data.endDate')" prop="endDate" width="auto"></el-table-column>
				<el-table-column :label="$t('table_data.operate')" width="200">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_detail(scope.row, scope.$index)">{{ $t('table_data.detail') }}</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">{{ $t('table_data.edit') }}</el-button>
							<el-button text type="danger" size="small" v-if="!scope.row.status" @click="table_del(scope.row, scope.$index)">{{ $t('table_data.del') }}</el-button>
							<el-button text type="info" size="small" icon="el-icon-view" @click="table_view(scope.row.movieId)">{{ $t('table_data.view') }}</el-button>
							<!-- <el-popconfirm :title="$t('table_data.del_text')" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">{{ $t('table_data.del') }}</el-button>
								</template>
							</el-popconfirm> -->
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<!-- <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog> -->

</template>

<script>
	// import saveDialog from './client/save'

	export default {
		name: "client",
		components: {
			// saveDialog
		},
		data(){
			return {
				langList: [
					{
						name: '简体中文',
						value: 'zh_CN',
					},
					{
						name: '繁體中文',
						value: 'zh_TW',
					},
					{
						name: 'English',
						value: 'en_US',
					},
					{
						name: '日本語',
						value: 'ja_JP',
					}
				],
				statusList: [
					{
						name: this.$t('movie_data.movieStatus.status1'),
						value: 0,
					},
					{
						name: this.$t('movie_data.movieStatus.status2'),
						value: 1,
					},
					{
						name: this.$t('movie_data.movieStatus.status3'),
						value: 2,
					}
				],
				dialog: {
					save: false
				},
				search: {
					keyword: ""
				},
				apiObj: {},
				selection: [],
				searchKey: null,
				params: {
					movieStatus: null,
					startDate: null,
					endDate: null,
					language: null,
					userName: this.searchKey,
					email: this.searchKey,
					phoneNo: this.searchKey,
					fullName: this.searchKey
				}
			}
		},
    async created(){
			this.apiObj = this.$API.creator_movie.getMovie;
    },
		methods: {
			// 预览
			table_view(id=''){
				window.open(process.env.VUE_APP_WEBSITE + '/movie-view?id=' + id)
				// this.showArtwork = true
			},
			reset(){
				this.params.status = null;
				this.params.userType = null;
				this.params.language = null;
				this.params.startDate = null;
				this.params.endDate = null;
				this.$refs.table.upData(this.params);
			},
			upsearch(){
				this.$refs.table.upData(this.params);
			},
			//增加
			add(){
				this.$router.push({path:'/movieTable/client/save',query:{type:'add','id':'add'}})
				// this.$router.push({path:'/userTable/client/save',query:{type:'add','id':row.userId}})
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open()
				// })
			},
			//编辑
			table_edit(row){
				this.$router.push({path:'/movieTable/client/save',query:{type:'add','id':row.movieId}})
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open('edit').setData(row)
				// })
			},
			// 详情
			table_detail(row){
				this.$router.push({path:'/movieTable/client/save',query:{type:'detail','id':row.movieId}})
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open('edit').setData(row)
				// })
			},
			//删除
			async table_del(row, index=0){
				console.log(index)
				this.$confirm(this.$t('table_data.deltips1'), this.$t('table_data.tips'), {
          confirmButtonText: this.$t('table_data.editText'),
          cancelButtonText: this.$t('table_data.cancelText'),
          type: 'warning'
        }).then(async () => {
					var res = await this.$API.creator_movie.delMovie.delete(row.movieId);
          if(res.code == 'OK'){
						//这里选择刷新整个表格 OR 插入/编辑现有表格数据
						this.$refs.table.upData(this.params);
						this.$message.success(this.$t('table_data.delsucc'))
					}else{
						this.$alert(res.message, this.$t('table_data.tips'), {type: 'error'})
					}
        }).catch(() => {
          this.$message({
            type: 'info',
            message: 'cancel'
          });          
        });
			},
			//批量删除
			async batch_del(){
				this.$confirm(`${this.$t('table_data.deltips1')} ${this.selection.length} ${this.$t('table_data.deltips2')}`, this.$t('table_data.tips'), {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success(this.$t('table_data.operasucc'))
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				console.log(1111111,selection)
				this.selection = selection;
			},
			//本地更新数据
			handleSuccess(data, mode){
				if(mode=='add'){
					data.id = new Date().getTime()
					this.$refs.table.tableData.unshift(data)
				}else if(mode=='edit'){
					this.$refs.table.tableData.filter(item => item.id===data.id ).forEach(item => {
						Object.assign(item, data)
					})
				}
			},
			beforeOptions(v){
				if(!this.params.endDate)return false
				return v.getTime() > new Date(this.params.endDate).getTime();//  - 86400000是否包括当天
			},
			backOptions(v){
				return v.getTime() < new Date(this.params.startDate).getTime();//  - 86400000是否包括当天
			},
		}
	}
</script>

<style scoped>
.module-filter {
	margin-right: 10px;
}
.el-header {
	--el-header-height: auto;
}
.left-panel {
	flex-wrap: wrap;
	line-height: 50px;
}
.top-panel {
	margin: 10px;
}
.scTable {
	height: calc(100% - 60px) !important;
}
.el-main.nopadding {
	margin: 0 10px;
}
.el-header {margin: 10px;border-radius: 4px;}
</style>
